﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using SHRIS.MIS.Entity.ScadaData;
using SHRIS.MIS.IService.APIExternal;
using SHRIS.MIS.Model.Param.APIExternal;
using SHRIS.MIS.Service.LinePlantService;
using SHRIS.MIS.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SHRIS.MIS.Web.Areas.DeviceManage.Controllers
{
    [Area("DeviceManage")]
    public class DeviceAPIController : BaseController
    {
        #region 服务注入
        private readonly IMapper _mapper;
        IDeviceStatusService _deviceStatusService;
        IDeviceAlarmService _deviceAlarmService;
        LinePlantService _LinePlantService;
        public DeviceAPIController(IMapper mapper, LinePlantService LinePlantService,IDeviceStatusService deviceStatusService, IDeviceAlarmService deviceAlarmService)
        {
            _mapper = mapper;
            _deviceStatusService = deviceStatusService;
            _deviceAlarmService = deviceAlarmService;
            _LinePlantService = LinePlantService;
        }

        #endregion

        #region API服务
        /// <summary>
        /// 获取产线基本预览
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetDevicePreview()
        {
            /**
             * 上下料工位的物料吗
             * 地轨机器人的状态、物料吗、孔深数据
             * 孔深测量设备的状态、物料、孔深数据
             * 缓存台中的物料吗
             * 激光打码设备状态、物料、打码内容
             * 车床状态、物料、孔深数据、主轴数据、刀具数据
             * 三坐标测量仪状态、物料、测量数据
             */
            // 前往设备实时信息参数表，将以上设备查询出来
            ResponseDto resultEntity = new ResponseDto();
            resultEntity.SerialID = Guid.NewGuid().ToString();
            resultEntity.Status = "s";
            return Json(resultEntity);
        }

        /// <summary>
        /// 获取设备详情
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetDeviceDetail([FromBody] object data)
        {
            string obj_status = "fail";
            string obj_message = "";
            var test = new { };
            var _data = new {
                _test=test
            };
            try
            {
                /**
                 * 1 实时状态获取
                 */
                // 获取设备状态
                string deviceCode = "";
                var status = await _deviceStatusService.SearchDeviceStatus(deviceCode);
                DeviceStatusDto statusObj = (DeviceStatusDto)_mapper.Map<IEnumerable<DeviceStatusDto>>(status);

                var alarms = await _deviceAlarmService.SearchDeviceAlarm(deviceCode);
                var alarmsObj = _mapper.Map<IEnumerable<DeviceAlarmDto>>(status);
                // 获取设备数据

                /**
                 * 2 当班历史状态获取
                 */
                DateTime nowd = DateTime.Now;
                int year = nowd.Year;
                int month = nowd.Month;
                int day = nowd.Day;
                DateTime stime = new DateTime(year, month, day, 0, 0, 0, 0);
                DateTime etime = new DateTime(year, month, day, 23, 59, 59, 999);

                var todayStatus = await _deviceStatusService.SearchDeviceStatus(deviceCode, stime, etime);
                var todayStatusObj = _mapper.Map<IEnumerable<DeviceStatusDto>>(todayStatus);

                var todayAlarm = await _deviceAlarmService.SearchDeviceAlarm(deviceCode, stime, etime);
                var todayAlarmObj = _mapper.Map<IEnumerable<DeviceAlarmDto>>(todayAlarm);
                obj_status = "success";
                //_data = new
                //{
                //    _statusObj= statusObj,

                //};
            }
            catch(Exception ex)
            {
                obj_status = "fail";
                obj_message = ex.Message;
            }
            

            var obj = new
            {
                status=obj_status,
                message=obj_message,
                _data= _data
            };
            return Json(obj);
        }

        /// <summary>
        /// 获取设备历史状态
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetDeviceHistory([FromBody] object data)
        {
            ResponseDto resultEntity = new ResponseDto();
            resultEntity.SerialID = Guid.NewGuid().ToString();
            resultEntity.Status = "s";
            return Json(resultEntity);
        }

        /// <summary>
        /// 提交设备记录信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> PostDeviceInfo([FromBody] object data)
        {
            ResponseDto resultEntity = new ResponseDto();
            resultEntity.SerialID = Guid.NewGuid().ToString();
            resultEntity.Status = "s";
            return Json(resultEntity);
        }

        #endregion
    }
}
